
package com.axxc.jpa.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.axxc.jpa.entity.HumanEntity;
import com.axxc.jpa.out.filter.HumanFilterHasDelete;
import com.axxc.jpa.repository.IHumanRepository;
import com.axxc.jpa.service.AbstractCRUDService;
import com.axxc.jpa.service.IHumanService;
import com.axxc.jpa.vo.HumanVO;

/**
 * @ClassName: PersonServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 熊小川
 * @date 2018年5月12日 上午10:58:44
 */
@Service
public class HumanServiceImpl extends AbstractCRUDService<HumanVO, HumanEntity, HumanVO, Long>
		implements IHumanService {

	@Autowired
	private IHumanRepository personRepository;

	@Override
	// unless 排除结果<0,即>0才缓存
	@Cacheable(value = "AllHumanEntity", unless = "#result.getSize() == 0")
	public Page<HumanEntity> listPersonAndPage(Specification<HumanEntity> filter, Pageable pageable) {
		logger.info("缓存没有,查询数据库!");
		Page<HumanEntity> page = personRepository.findAll(filter, pageable);
		return new PageImpl<>(page.getContent(), pageable, page.getTotalElements());
	}

	@Override
	protected HumanEntity toEntity(HumanVO target) {
		// TODO Auto-generated method stub
		return target.toEntity();
	}

	@Override
	protected HumanVO toModel(HumanEntity entity) {
		// TODO Auto-generated method stub
		return entity.toModel();
	}

	@Override
	protected Specification<HumanEntity> toFilter(HumanVO target) {
		// TODO Auto-generated method stub
		return new HumanFilterHasDelete(target);
	}

	@Override
	@Cacheable(key = "#id", value = "HumanEntity")
	public HumanEntity findOne(Long id) {

		logger.info("缓存不存在,查询数据库.ID为" + id);
		return personRepository.findOne(id);
	}

}
